package com.fast.manager.base.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fast.commons.spring.SpringContextHolder;
import com.fast.commons.web.model.AjaxResponse;
import com.fast.commons.web.model.DataGrid;
import com.fast.manager.base.domain.Role;
import com.fast.manager.base.domain.RoleMenu;
import com.fast.manager.base.repository.RoleMapper;
import com.fast.manager.base.repository.RoleMenuMapper;
import com.fast.manager.base.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleMapper mapper ;
	
	@Autowired
	private RoleMenuMapper roleMenuMapper ;
	
	@Override
	public AjaxResponse add(Role record) {
		if(this.mapper.existsFileldValue(null, "fullName", record.getFullName()) > 0) 
			return AjaxResponse.failMsg("该角色已存在！") ;
		if(this.mapper.existsFileldValue(null, "code", record.getCode()) > 0) 
			return AjaxResponse.failMsg("该代码已存在！") ;
			
		this.mapper.insertSelective(record);
		
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse delete(long id) {
		this.mapper.deleteByPrimaryKey(id);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse deleteBatch(Long[] ids) {
		this.mapper.deleteByPrimaryKeys(ids);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse update(Role record) {
		if(this.mapper.existsFileldValue(record.getId(), "fullName", record.getFullName()) > 0) 
			return AjaxResponse.failMsg("该角色已存在！") ;
		if(this.mapper.existsFileldValue(record.getId(), "code", record.getCode()) > 0) 
			return AjaxResponse.failMsg("该代码已存在！") ;
		
		this.mapper.updateByPrimaryKeySelective(record);
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public Role get(long id) {
		return this.mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Role> getAllList(Role record) {
		return this.mapper.findAllList(record);
	}
	
	@Override
	public DataGrid<Role> datagrid(Role conditions) {
		DataGrid<Role> dg = new DataGrid<Role>();
		
		Map<String, Object> conditionsMap = new HashMap<String, Object>();
		conditionsMap.put("record", conditions);
		conditionsMap.put("offset", dg.getOffset());
		conditionsMap.put("rows", dg.getPageSize());

		List<Role> list = this.mapper.findAllListPage(conditionsMap);
		int count = this.mapper.getCount(conditionsMap);
		dg.setRows(list);
		dg.setTotal(count);

		return dg;
	}

	@Override
	public List<Role> findRoleByIds(Long[] roleIds) {
		return this.mapper.findRoleByIds(roleIds);
	}

	@Override
	public void addPermitToRole(Long roleId, Long[] menu_ids, String permit_ids) {
		//删除原有角色权限
		this.roleMenuMapper.deleteByRoleId(roleId) ;
		
		//添加权限到角色
		for(int i=0; i<menu_ids.length; i++) {
			
			//如果选择菜单而不选择操作权限，则不添加
			if(null != permit_ids && !"".equals(permit_ids.trim())) {
				String[] permits = permit_ids.split("\\|");
				
				RoleMenu rm = new RoleMenu() ;
				rm.setRole_id(roleId);
				rm.setMenu_id(menu_ids[i]);
				rm.setPermit_ids(permits[i]);
				this.roleMenuMapper.insert(rm) ;
			}
		}
	}

	@Override
	public List<RoleMenu> findRoleMenuByRoleId(Long roleId) {
		return this.roleMenuMapper.findRoleMenuByRoleId(roleId);
	}

	@Override
	public List<Role> getUserRolesById(Long user_id) {
		return this.mapper.getUserRolesById(user_id) ;
	}

}
